package com.wrupple.muba.widget.client.widgets.toolbar;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.cobogw.gwt.user.client.CSS;

import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.DockLayoutPanel.Direction;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.wrupple.muba.widget.client.widgets.panels.CenteredPanel;
import com.wrupple.muba.widget.client.widgets.panels.LayoutGrid;

public class ActionToolbar extends ResizeComposite implements Toolbar {

	static final int CELL_SIZE = 48;
	List<Widget> actions;
	CenteredPanel centered;
	LayoutPanel panel;
	public ActionToolbar() {
		this.actions = new ArrayList<Widget>();
		SimplePanel background = new SimplePanel();
		background.setStyleName("action-toolbar-background");
		panel = new LayoutPanel();
		panel.add(background);
		initWidget(panel);
	}
	
	public void addActions(Collection<? extends Widget> newActions){
		actions.addAll(newActions);
		if(centered!=null){
			centered.removeFromParent();
		}
		LayoutGrid grid = new LayoutGrid(actions.size(), CELL_SIZE, CELL_SIZE);
		CSS.setProperty(grid, "marginTop", "5px");
		centered = new CenteredPanel(grid, CELL_SIZE
				* actions.size()+20);
		
		
		panel.add(centered);
		for (Widget w : this.actions) {
			grid.add(w);
		}
	}
	
	

	@Override
	public HandlerRegistration addResizeHandler(ResizeHandler handler) {
		return super.addHandler(handler, ResizeEvent.getType());
	}

	@Override
	public Direction getDirection() {
		return Direction.SOUTH;
	}

	@Override
	public int getSize() {
		return 60;
	}

}
